﻿/**
 * Created by IntelliJ IDEA.
 * User: terminator
 * Date: 8/13/11
 * Time: 2:56 PM
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function(){
    $("#confirmPass").keypress(function(){
        var $this=$(this);
        if($this.next().val()==null){
            return;
        }else{
            $this.val("");
            $this.next().remove();
        }
    }) ;
    var oldName=$("#modifyUser").val();
    $("#modifyUser").change(function(){
        var $this=$(this);
        if($this.val()==""){
            $this.next().remove();
            $this.after("<span style='color:red;'>请输入用户名</span>");
        }
        else{
//            var url=$("#actionPath").val()+"?username="+$this.val();
            if($this.val()!=oldName){
                var url=actionPath+"?username="+$this.val();
                $.ajax({
                    url:url,
                    type:"GET",
                    dataType:"text",
                    success:function(data){
                        if(data=="false"){
                            $this.next().remove();
                            $this.after("<span style='color:red;'>此用户名已存在</span>");

                        }else{
                            $this.next().remove();
                        }
                    }
                });
            }
            else{
                $this.next().remove();
            }
        }
    });

    $("#username").blur(function(){
        var $this=$(this);
        if($this.val()==""){
            $this.next().remove();
            $this.after("<span style='color:red;'>请输入用户名</span>");
        }
        else{
//            var url=$("#actionPath").val()+"?username="+$this.val();
            var url=actionPath+"?username="+$this.val();
            $.ajax({
                url:url,
                type:"GET",
                dataType:"text",
                success:function(data){
                    if(data=="false"){
                        $this.next().remove();
                        $this.after("<span style='color:red;'>此用户名已存在</span>");

                    }else{
                        $this.next().remove();
                    }
                }
            });
        }
    });

    $("#oldPwd").blur(function(){
        var $this=$(this);
        if($this.val()==""){
            $this.next().remove();
            $this.after("<span style='color:red;'>请输入原始密码</span>");
        }
        else{
            var url=actionPwdPath,data="oldPassword="+$this.val()+"&id="+userId;
            $.ajax({
                url:url,
                type:"post",
                dataType:'text',
                data:data,
                success:function(txt){
                    if(txt=="false"){
                        $this.next().remove();
                        $this.after("<span style='color:red;'>原始密码不正确</span>");
                    }
                    else{
                        $this.next().remove();
                    }
                }
            })
        }
    });

    $("#pass").blur(function(){
        var $this=$(this),reg=/^[a-zA-Z0-9]{6,12}$/;
        if($this.val()==""){
            $this.next().remove();
            $this.after("<span style='color:red;'>请输入密码</span>");
        }
        else if(!reg.test($this.val())){
            $this.next().remove();
            $this.after("<span style='color:red;'>密码只能由6-12位数字或字母组组成</span>")
        }
        if($("#confirmPass").val()!=""){
            if($this.val()!=$("#confirmPass").val()){
                $("#confirmPass").next().remove();
                $("#confirmPass").after("<span style='color:red;'>两次输入的密码不匹配</span>");
            }
        }
    });
    $("#confirmPass").blur(function(){
        var $this=$(this),reg=/^[a-zA-Z0-9]{6,12}$/;
        var pass=$("#pass").val();
        if(pass==""){
            $("#pass").next().remove();
            $("#pass").after("<span style='color:red;'>你还没有填写密码</span>");
        }
        else if($this.val()==""&&pass!=""){
            $this.next().remove();
            $this.after("<span style='color:red;'>请确认密码</span>");
        }else if(!reg.test($this.val())){
            $this.next().remove();
            $this.after("<span style='color:red;'>密码只能由6-12位数字或字母组组成</span>")
        }else if($this.val()!=pass){
            $this.next().remove();
            $this.after("<span style='color:red;'>两次输入的密码不匹配</span>");
        }
    }) ;
    $("#email").blur(function(){
        var $this=$(this);
        if($this.val()!=""){
            var reg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
            if(!reg.test($this.val())){
                $this.next().remove();
                $this.after("<span style='color:red;'>Email的格式不正确</span>");
            }else{
                $this.next().remove();
            }
        }
    });
    $("#tele").blur(function(){
        var $this=$(this);
        if($this.val()!=""){
            var reg=/^\d{8}$|^\d{11}$/;
            if(!reg.test($this.val())){
                $this.next().remove();
                $this.after("<span style='color:red;'>电话号码的格式不正确</span>")
            } else{
                $this.next().remove();
            }
        }
    });
    $("#submit").click(function(){
        if($("#username").val()==""||$("#pass").val()==""||$("#confirmPass").val()==""){
            $("#submit").next().remove();
            $("#submit").after("<span style='color:red'>用户名,登录密码,确认密码为必填项</span>");
            return false;
        }
//        else if($("#username").next().val()!=null||$("#pass").next.val()!=""||$("#confirmPass").next.val()!=""||$("#email").next.val()!=""){
        else if($("#username,#pass,#confirmPass,#email").next().val()!=null){
            $("#submit").next().remove();
            $("#submit").after("<span style='color:red'>请按要求填写表单</span>");
            return false;
        }else{
//            var url=$("#registerPath").val();
            var url=registerPath;
            var data="user.username="+$("#username").val()+"&user.password="+$("#pass").val()+"&confirmPass="+$("#confirmPass").val()+"&user.name="+$("#name").val()+"&user.email="+$("#email").val()+"&user.sex="+$(".sex:checked").val()+"&user.tele="+$("#tele").val()+"&user.addr="+$(".address").val();
            $.ajax({
                url:url,
                type:"post",
                dataType:"text",
                data:data,
                success:function(obj){
//                    if(obj=="true"){
//                        con=confirm("继续注册？");
//                        if(con){
//                            $("#username,#pass,#confirmPass,#name,#email,#tele,.address").val("");
//                        }else{
//                            /*跳转向首页*/
////                            location.href=$("#redirectPath").val();
//                            location.href=redirectUrl;
//                        }
                    if(obj=="true"){
                        alert("注册成功");
                        location.href=redirectUrl;
                    }else{
                        alert("注册失败,请稍后重试");
                        return false;
                    }
                }
            });
        }
    });

    $("#subModify").click(function(){
        if($("#oldPwd").val()==""||$("#pass").val()==""||$("#confirmPass").val()==""){
            $("#subModify").next().remove();
            $("#subModify").after("<span style='color:red;'>原始密码,新密码,确认密码为必填项</span>");
            return false;
        }else if($("#oldPwd,#pass,#confirmPass,#email").next().val()!=null){
            $("#submit").next().remove();
            $("#submit").after("<span style='color:red'>请按要求填写表单</span>");
            return false;
        }else{
            var url=modifyPath;
            var data="id="+userId+"&user.username="+$("#modifyUser").val()+"&oldPassword="+$("#oldPwd").val()+"&user.password="+$("#pass").val()+"&confirmPass="+$("#confirmPass").val()+"&user.name="+$("#name").val()+"&user.email="+$("#email").val()+"&user.sex="+$(".sex:checked").val()+"&user.tele="+$("#tele").val()+"&user.addr="+$(".address").val();
            $.ajax({
                url:url,
                data:data,
                type:"post",
                dataType:"text",
                success:function(txt){
                    if(txt=="true"){
                        alert("修改成功！");
                        location.href=managerUrl;
                    }else{
                        alert("修改失败,请检查完善信息！");
                        return false;
                    }
                }
            })
        }
    });
});
